package com.pyg.manager.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pyg.manager.service.SpecificationService;
import com.pyg.mapper.TbSpecificationMapper;
import com.pyg.mapper.TbSpecificationOptionMapper;
import com.pyg.pojo.*;
import com.pyg.vo.Specification;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

@Service
public class SpecificationServiceImpl implements SpecificationService{

    @Autowired
    private TbSpecificationMapper specificationMapper;

    @Autowired
    private TbSpecificationOptionMapper specificationOptionMapper;

    @Override
    public PageResult findByPage(Integer pages, Integer rows) {

        PageHelper.startPage(pages,rows);
//        find All the specification
        Page<TbSpecification> specifications = (Page<TbSpecification>) specificationMapper.selectByExample(null);
        return new PageResult(specifications.getResult(),specifications.getTotal());
    }

    @Override
    public Specification findOne(Long id) {
//        find the specification by id
        TbSpecification specification = specificationMapper.selectByPrimaryKey(id);

//        查询TbSpecification信息
        TbSpecificationOptionExample example = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
        criteria.andSpecIdEqualTo(id);
        List<TbSpecificationOption> specificationOptions = specificationOptionMapper.selectByExample(example);

//        封装并且返回数据
        return new Specification(specification,specificationOptions);
    }

    @Override
    public PygResult add(Specification specification) {

        try {
//            insert to TbSpecification
            specificationMapper.insert(specification.getSpecification());

//            foreach the options of specificationOption and insert
            for(TbSpecificationOption option : specification.getSpecificationOptionList()){
                option.setSpecId(specification.getSpecification().getId());
                specificationOptionMapper.insertSelective(option);
            }

            return new PygResult(true,"add succeed");
        } catch (Exception e) {
            e.printStackTrace();
            return new PygResult(false,"add failed");
        }


    }

    @Override
    public PygResult update(Specification specification) {
        try {
//        update the TbSpecification
            specificationMapper.updateByPrimaryKey(specification.getSpecification());

//        delete all the TbSpecificationOption
            TbSpecificationOptionExample example = new TbSpecificationOptionExample();
            TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
            criteria.andSpecIdEqualTo(specification.getSpecification().getId());
            specificationOptionMapper.deleteByExample(example);

//        add the new specificationOptions
            for(TbSpecificationOption option : specification.getSpecificationOptionList()){
                option.setSpecId(specification.getSpecification().getId());
                specificationOptionMapper.insertSelective(option);
            }
            return new PygResult(true,"update Success");
        } catch (Exception e) {
            e.printStackTrace();
            return new PygResult(false,"update failed");
        }
    }

    @Override
    public PygResult delete(Long[] ids) {
        try {
            for(Long id : ids){
//                delete the options by spec_id
                TbSpecificationOptionExample example = new TbSpecificationOptionExample();
                TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
                criteria.andSpecIdEqualTo(id);
                specificationOptionMapper.deleteByExample(example);
//                delete the specification by id
                specificationMapper.deleteByPrimaryKey(id);
            }
            return new PygResult(true,"delete success");
        } catch (Exception e) {
            e.printStackTrace();
            return new PygResult(false,"delete failed");
        }
    }

    @Override
    public List<Map> findSpecList() {
        return specificationMapper.findSpecList();
    }
}
